from spass.api.api_db import (info_user,
                              info_team,
                              info_teamset)
from spass.core.error import error
from spass.core.message import message
from optparse import OptionParser

def callback_user(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    fullname = parser.values.user[0]
    name = parser.values.user[1]
    result = info_user(fullname, name)
    if result == 1:
        return 1
    message('user.found', [fullname+" "+name])
    message('user.infos.recap', [fullname, name, str(len(result[0])), str(len(result[1]))])

    if not len(result[0]) == 0:
        message('team.tag', [])
        for team in result[0]:
            print team.id_team
    if not len(result[1]) == 0:
        message('assignment.tag', [])
        for assignment in result[1]:
            message('infos.details', [str(assignment.id), str(assignment.id_template), str(assignment.date), str(assignment.id_teamset), str(assignment.id_user)] )

def callback_team(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    id_team = parser.values.team
    result = info_team(id_team)
    if result == 1:
        return 1
    message('team.found', [str(id_team)])
    message('team.infos.recap', [str(id_team), str(len(result[0])), str(len(result[1])) ])
    
    if not len(result[0]) == 0:
        message('teamset.tag', [])
        for teamset in result[0]:
            print teamset.id_team_set
    if not len(result[1]) == 0:    
        message('assignment.tag', []) 
        for assignment in result[1]:
            message('infos.details', [str(assignment.id), str(assignment.id_template), str(assignment.date), str(assignment.id_teamset), str(assignment.id_user)])

def callback_teamset(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    id_teamset = parser.values.teamset
    result = info_teamset(id_teamset)
    if result == 1:
        return 1
    message('teamset.found', [str(id_teamset)])
    message('teamset.infos.recap', [str(id_teamset), str(len(result))])
    
    if not len(result) == 0:   
        message('assignment.tag', [])
        for assignment in result:
            message('infos.details', [str(assignment.id), str(assignment.id_template), str(assignment.date), str(assignment.id_teamset), str(assignment.id_user)])

def info(argv):
    if len(argv) < 1:
        error('args.need', ['info', ''])
        return 1

    parser = OptionParser()
    parser.set_usage("""spass info <options...>""")
    parser.add_option("--user", action="callback", callback=callback_user, nargs=2, type="string", dest="user",
                      help="Info about an user. ARG : <first name> <second name>")  
    parser.add_option("--team", action="callback", callback=callback_team, nargs=1, type="int", dest="team",
                      help="Info about a team. ARG : <team ID>")
    parser.add_option("--teamset", action="callback", callback=callback_teamset, nargs=1, type="int", dest="teamset",
                      help="Info about a teamset. ARG : <teamset ID>")

    (options, args) = parser.parse_args(argv)
    return 0
